/**
 * 
 */
package pl.edu.agh.to;

import java.util.List;

import android.app.AlertDialog;
import android.os.Bundle;

/**
 * @author karulis
 * 
 */
public class ParticipantsActivity extends TemplateActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO add dialog for removing from course
		super.onCreate(savedInstanceState);
	}

	@Override
	int getLayoutResID() {
		return R.layout.participants;
	}

	@Override
	int getSpinnerID() {
		return R.id.participants_spinner;
	}

	@Override
	int getListViewID() {
		return R.id.participants_list;
	}

	@Override
	List<Object> listViewContent() {
		return getdBAccess().getCourses(getSpinnerSelectedItem());
	}

	@Override
	List<Object> spinnerContent() {
		return getdBAccess().getAllParticipants();
	}

	@Override
	protected int getMenuRes() {
		return R.menu.participants_menu;
	}

	@Override
	protected void scheduleChange() {
		// do nothing
	}

	@Override
	protected void attendChange() {
		setupList();
	}

	@Override
	protected void rateChange() {
		// do nothing
	}

	@Override
	protected void courseChange() {
		setupList();
	}

	@Override
	protected void participantChange() {
		setupSpinner();
	}
}

class ParticipantRemoveDialog extends RemoveDialog {
	ParticipantRemoveDialog(TemplateActivity context) {
		super(context);
	}

	@Override
	protected int getMessageId() {
		return R.string.participants_dialog_text;
	}

	@Override
	protected void action() {
		getdBAccess().removeParticipant(getSpinnerItem());
	}
}

class ParticipantEditDialog extends ParticipantAddDialog {
	Participant participant;

	ParticipantEditDialog(TemplateActivity context) {
		super(context);
	}

	/**
	 * @return
	 */
	@Override
	protected int layoutId() {
		return R.layout.participants_dialog;
	}

	@Override
	protected void prepareDialog() {
		participant = getdBAccess().getParticipant(
				getActivity().getSpinnerSelectedItem());
		getUpperTextEdit().setText(participant.getName());
		getLowerTextEdit().setText(participant.getSurname());
	}

	@Override
	protected String getBaseMessage() {
		return getContext().getResources()
				.getText(R.string.participants_edit_dialog_text).toString();
	}

	@Override
	protected void action() {
		getdBAccess().updateParticipant(
				participant,
				new Participant(getUpperTextEdit().getText().toString(),
						getLowerTextEdit().getText().toString()));
	}

}

class ParticipantAddDialog extends AddDialog {

	@Override
	protected void prepareDialog() {
		getUpperTextEdit().setText("");
		getLowerTextEdit().setText("");
		getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
	}

	/**
	 * @return
	 */
	@Override
	protected int layoutId() {
		return R.layout.participants_dialog;
	}

	@Override
	protected String getBaseMessage() {
		return getContext().getResources()
				.getText(R.string.participants_add_dialog_text).toString();
	}

	ParticipantAddDialog(TemplateActivity context) {
		super(context);
	}

	@Override
	protected void action() {
		getdBAccess().addParticipant(
				new Participant(getUpperTextEdit().getText().toString(),
						getLowerTextEdit().getText().toString()));
	}

	/**
	 * @return
	 */
	@Override
	protected boolean enableButton() {
		return getUpperTextEdit().getText().toString().isEmpty()
				|| getLowerTextEdit().getText().toString().isEmpty();
	}

	@Override
	protected int upperTextEditId() {
		return R.id.participant_name_edit;
	}

	@Override
	protected int lowerTextEditId() {
		return R.id.participant_surname_edit;
	}

}

class AddParticipantToDialog extends AddToDialog {

	AddParticipantToDialog(TemplateActivity activity) {
		super(activity);
	}

	@Override
	protected int getViewId() {
		return R.layout.add_participant_to_dialog;
	}

	@Override
	protected List<Object> getSpinnerContent() {
		return getdBAccess().getAllCourses();
	}

	@Override
	protected int getDialogSpinnerId() {
		return R.id.add_participant_to_spinner;
	}

	@Override
	protected int getMessageId() {
		return R.string.participants_add_to_dialog_text;
	}

	@Override
	protected void action() {
		getdBAccess().addParticipantToCourse(getSpinnerItem(),
				getDialogSpinnerItem());
	}

}

class RemoveParticipantFromDialog extends AddToDialog {

	RemoveParticipantFromDialog(TemplateActivity activity) {
		super(activity);
	}

	/**
	 * @return
	 */
	@Override
	protected int getViewId() {
		return R.layout.remove_participant_from_dialog;
	}

	/**
	 * @return
	 */
	@Override
	protected List<Object> getSpinnerContent() {
		return getdBAccess().getCourses(getSpinnerItem());
	}

	@Override
	protected int getMessageId() {
		return R.string.participants_remove_from_dialog_text;
	}

	/**
	 * @return
	 */
	@Override
	protected int getDialogSpinnerId() {
		return R.id.remove_participant_from_spinner;
	}

	@Override
	protected void action() {
		getdBAccess().removeParticipantFromCourse(getSpinnerItem(),
				getDialogSpinnerItem());
	}
}